﻿#region Using

using System;
using System.Diagnostics;

#endregion Using

namespace T4_Eind_programma
{
    public class ItemInfo
    {
        #region Variables

        private bool active;
        private Stopwatch activeTimer = new Stopwatch();
        private string filteredName;
        private object itemValue;
        private string name;
        private Stopwatch programActiveTimer = new Stopwatch();

        #endregion Variables

        #region Constructor

        public ItemInfo(string name, object value)
        {
            this.name = name;
            this.itemValue = value;
            filteredName = FilterName(name);
        }

        #endregion Constructor

        #region Properties

        public bool Active
        {
            get
            {
                return active;
            }
            set
            {
                active = value;
                if (active == true)
                {
                    if (activeTimer.IsRunning == false)
                        activeTimer.Start();
                }
                else
                {
                    activeTimer.Stop();
                }
            }
        }

        public string FilteredName
        {
            get
            {
                return filteredName;
            }
            set
            {
                filteredName = value;
            }
        }

        public object ItemValue
        {
            get
            {
                return this.itemValue;
            }
            set
            {
                this.itemValue = value;
                if (Convert.ToBoolean(itemValue) == true)
                {
                    active = true;
                }
                else
                {
                    active = false;
                }
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public Stopwatch ProgramActiveTimer
        {
            get { return programActiveTimer; }
            set { programActiveTimer = value; }
        }

        public Stopwatch Timer
        {
            get { return activeTimer; }
            set { activeTimer = value; }
        }

        #endregion Properties

        public string BerekenEffectiviteit()
        {
            int percentage = 0;

            if (activeTimer.ElapsedMilliseconds > 0)
            {
                percentage = ((int)activeTimer.ElapsedMilliseconds / (int)programActiveTimer.ElapsedMilliseconds) * 100;
            }

            return percentage + "%";
        }

        private string FilterName(string name)
        {
            string[] splitter = name.Split('>');

            return splitter[splitter.Length - 1];
        }
    }
}